//
//  XIndexPage.m
//  food
//
//  Created by XmaMini on 2019/4/15.
//  Copyright © 2019年 XmaMini. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "XIndexPage.h"
#import "ReadTool.h"
#import "XInfoPage.h"


@implementation XIndexItemView

//
- (void)updateCellAppearanceWithIndexPath:(NSIndexPath *)indexPath {
    [super updateCellAppearanceWithIndexPath:indexPath];
   
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.mImageView.bounds cornerRadius:QMUICMI.searchBarTextFieldCornerRadius];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = self.mImageView.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    self.mImageView.layer.mask = maskLayer;

}





- (void)setFrame:(CGRect)frame
{
    CGRect bounds = [UIScreen mainScreen].bounds;
    frame.origin.x = 10;
    frame.size.width=bounds.size.width - frame.origin.x *2; 
    [super setFrame:frame];
}
@end


@implementation XIndexPage


- (void)initSubviews{
    [super initSubviews];
    NSDictionary *config =[ReadTool readLocalFileWithName:@"config"];
    if(config!=nil){
        self._config =[config objectForKey:@"index"];
        self._title = [config objectForKey:@"title"];
        
        if(self.showType != 0){
            self.showCount = 0;
            for(int i = 0;i<self._config.count;i++){
                int type = [[[self._config objectAtIndex:i] valueForKey:@"type"] intValue];
                if(type == (self.showType - 1)){
                    self.showCount ++;
                }
            }
        }else{
            self.showCount = (int)self._config.count;
        }
    }
    self.title = self._title;
    
    if(self.showType == 1){
        self.title = @"早餐";
    }else if(self.showType == 2){
        self.title = @"午餐";
    }else if(self.showType == 3){
        self.title = @"晚餐";
    }
}

//- (void)viewDidLoad{
//    [super viewDidLoad];
//    NSLog(@"viewDidLoad  showtype = %d",self.showType);
//
//}




//- (void)setupNavigationItems {
//    [super setupNavigationItems];
//    self.title = self._title;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self._config.count;
    
    return self.showCount;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}


//去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 10; //sectionHeaderHeight
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
    }
}


//将图片处理成圆的
-(id)changeimagetocilce:(UIImage*)image
{
    
    UIImage *finalImage = nil;
    UIGraphicsBeginImageContext(image.size);
    {
//        UIGraphicsBeginImageContext(CGSizeMake(100,100));
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGAffineTransform trnsfrm = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(1.0, -1.0));
        trnsfrm = CGAffineTransformConcat(trnsfrm, CGAffineTransformMakeTranslation(0.0, image.size.height));
        CGContextConcatCTM(ctx, trnsfrm);
        CGContextBeginPath(ctx);
        CGContextAddEllipseInRect(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height));
        CGContextClip(ctx);
        CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height), image.CGImage);
        finalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    return finalImage;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *NODE_CELL_ID = @"IndexTableViewCellIdentifier";
    XIndexItemView * cell = [tableView dequeueReusableCellWithIdentifier:NODE_CELL_ID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"XIndexItem" owner:nil options:nil] firstObject];
    }

    cell = [[[NSBundle mainBundle] loadNibNamed:@"XIndexItem" owner:nil options:nil] firstObject];
    
    NSDictionary *dict = [self._config objectAtIndex:indexPath.section];
    if(self.showType != 0){
        int index = 0;
        for(int i = 0;i<self._config.count;i++){
            dict = [self._config objectAtIndex:i];
            int type = [[dict valueForKey:@"type"] intValue];
            if(type == (self.showType - 1)){
                if(index == indexPath.section){
                    [cell setTag:i];
                    break;
                }
                index ++;
            }
        }
    }
    
    cell.mImageView.image =  [self changeimagetocilce:[UIImage imageNamed:[dict valueForKey:@"img"]]];
    cell.mName.text = [dict valueForKey:@"name"];
    cell.mDesc.text = [dict valueForKey:@"desc"];
    cell.mName.textColor = QMUICMI.tabBarTintColor;
    cell.mDesc.textColor = QMUICMI.tabBarTintColor;
    [cell updateCellAppearanceWithIndexPath:indexPath];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    XInfoPage *viewController = [[XInfoPage alloc] init]; 
    [viewController setConfig:[self._config objectAtIndex:indexPath.section]]; 
    [self.navigationController pushViewController:viewController animated:YES];
    
}

@end
